package net.sddf.system.bank.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.sddf.system.bank.business.BankService;
import net.sddf.system.bank.business.model.dto.BankCardRequestDto;
import net.sddf.system.bank.business.model.vo.BankCardVo;
import net.sddf.system.bank.entity.BalanceDetails;
import net.sddf.system.bank.entity.User;
import net.sddf.system.bank.mapper.BalanceDetailsMapper;
import net.sddf.system.bank.mapper.UserMapper;
import net.sddf.system.bank.model.BalanceDetailsDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class BankServiceImpl extends ServiceImpl<UserMapper, User> implements BankService {

    @Autowired
    private BalanceDetailsMapper balanceDetailsMapper;

    @Override
    public BankCardVo createUser(BankCardRequestDto bankCardRequestDto) {
        BankCardVo bankCardVo = new BankCardVo();
        bankCardVo.setRequestNo(bankCardRequestDto.getReqCode());
        bankCardVo.setBankCode(bankCardRequestDto.getBankNo());
        bankCardVo.setBankName(bankCardRequestDto.getBankTitle());
        //校验个人信息
        //校验银行实体卡
        //保存开户信息
        //保存绑定银行卡信息
        //设置初始余额信息
        return bankCardVo;
    }

    @Override
    public BalanceDetailsDTO getBalanceDetails(String userCode) {

        //查询余额详情  降序 （同步id降序）
        BalanceDetails balanceDetails = balanceDetailsMapper.selectOne(
                new QueryWrapper<BalanceDetails>().lambda()
                        .eq(BalanceDetails::getUserCode, userCode)
                        .orderByDesc(BalanceDetails::getId).last("LIMIT 1")
        );


        //返回 余额
        if (balanceDetails != null) {
            BalanceDetailsDTO dto = new BalanceDetailsDTO();
            BeanUtils.copyProperties(balanceDetails, dto);
            return dto;
        }
        //返回一个 0
        BalanceDetailsDTO bd = new BalanceDetailsDTO();
        bd.setBalance(new BigDecimal("0"));
        return bd;
    }
}
